/**
 * ============================================================================
 *
 * Copyright (C) 2013 Android SCasia Systems.  All rights reserved. The content 
 * presented herein may not, under any circumstances, be reproduced, in 
 * whole or in any part or form, without written permission from 
 * SCasia Systems.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are NOT permitted. Neither the name of SCasia Systems,
 * nor the names of contributors may be used to endorse or promote products 
 * derived from this software without specific prior written permission.
 *
 * ============================================================================
 *
 * Author: Kelvin
 *  
 *
 * Revision History
 * ----------------------------------------------------------------------------
 * Date                Author              Comment, bug number, fix description
 *
 * January 20, 2013      Kelvin@futureworkz.com           version 1.0
 *
 * ----------------------------------------------------------------------------
 */
package arisufascion.com;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;
import arisufascion.com.action.ActionItem;
import arisufascion.com.action.QuickAction;
import arisufascion.com.activity.ActivityAboutUs;
import arisufascion.com.activity.ActivityCategories;
import arisufascion.com.activity.ActivityContactUs;
import arisufascion.com.activity.ActivityFaq;
import arisufascion.com.activity.ActivityPayment;
import arisufascion.com.activity.ActivityPolicy;
import arisufascion.com.activity.ActivityShipping;
import arisufascion.com.activity.ActivityWholesale;
import arisufascion.com.constant.Utils;
import arisufascion.com.tabs.TabGroupActivity;

// TODO: Auto-generated Javadoc
/**
 * The Class AbstractActivity.
 * 
 * @author Kelvin
 */
public class AbstractActivity extends Activity {

	/** The self. */
	public AbstractActivity self;

	/** The progress dialog. */
	ProgressDialog progressDialog;

	/** The res. */
	Resources res;

	/** The Constant TITLE. */
	public static final String TITLE = "tilte";

	/** The Constant LINK. */
	public static final String LINK = "link";

	/** The Constant TYPE. */
	public static final String TYPE = "type";

	/** The Constant IMAGE. */
	public static final String IMAGE = "image";
	public static final String MONAN = "monan";

	public static final String CATEGORIE = "categorie";

	public static final String CATEGORIE_ID = "categorie_id";

	public static final String PRODUCT = "product";

	/** The tag. */
	public static String TAG = "";

	/** The Constant GONE. */
	public static final int GONE = View.GONE;

	/** The Constant VISIBLE. */
	public static final int VISIBLE = View.VISIBLE;

	/** The Constant INVISIBLE. */
	public static final int INVISIBLE = View.INVISIBLE;

	public static final String CATEGORIESID = "categoriesId";
	public static final String CATEGORIES_NAME = "categories_name";
	public static final String DESCRIPTIONSCREEN_PREFIX = "descriptionscreen_prefix";

	public static final String DESCRIPTIONSCREEN_THUMBNAIL = "descriptionscreen_thumbnail";
	public static final String DESCRIPTIONSCREEN_TITLE = "descriptionscreen_title";
	public static final String DESCRIPTIONSCREEN_RECIPES = "descriptionscreen_recipes";
	public static final String DESCRIPTIONSCREEN_DESCRIPTION = "descriptionscreen_description";

	public static final String CATEGORIESACTIVITYS = "CategoriesActivitys";

	public static final String YEUTHICHACTIVITY = "YeuThichActivity";

	public static final String COMMENTACTIVITY = "CommentActivity";

	public static final String SELECTE_TAB = "selecte_tab";

	public static final String MONANACTIVITY = "MonAnActivity";
	QuickAction mQuickAction;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.requestWindowFeature(this);
		this.getWindow().getAttributes().windowAnimations = R.style.Animations_Activity;

		self = this;

		// About Us action item
		ActionItem aboutAction = new ActionItem();

		aboutAction.setTitle("About Us");
		aboutAction.setIcon(getResources().getDrawable(R.drawable.ic_about));

		// Products action item
		ActionItem productAction = new ActionItem();

		productAction.setTitle("Products");
		productAction.setIcon(getResources()
				.getDrawable(R.drawable.ic_products));

		// Payment action item
		ActionItem paymentAction = new ActionItem();

		paymentAction.setTitle("Payment");
		paymentAction
				.setIcon(getResources().getDrawable(R.drawable.ic_payment));

		// Payment action item
		ActionItem faqAction = new ActionItem();

		faqAction.setTitle("FAQ");
		faqAction.setIcon(getResources().getDrawable(R.drawable.ic_faq));

		// Payment action item
		ActionItem contactAction = new ActionItem();

		contactAction.setTitle("Contact Us");
		contactAction
				.setIcon(getResources().getDrawable(R.drawable.ic_contact));

		mQuickAction = new QuickAction(this);

		mQuickAction.addActionItem(aboutAction);
		mQuickAction.addActionItem(productAction);
		mQuickAction.addActionItem(paymentAction);
		mQuickAction.addActionItem(faqAction);
		mQuickAction.addActionItem(contactAction);

		// setup the action item click listener
		mQuickAction
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					@Override
					public void onItemClick(int pos) {

						if (pos == 0) { // Add item selected
							Intent intent_view_monan = new Intent(self,
									ActivityAboutUs.class);
							TabGroupActivity tabGroupActivityView = (TabGroupActivity) getParent();
							tabGroupActivityView.startChildActivity(
									"ActivityAboutUs", intent_view_monan);

						} else if (pos == 1) { // Accept item selected
							Intent intent_view_monan = new Intent(self,
									ActivityCategories.class);
							TabGroupActivity tabGroupActivityView = (TabGroupActivity) getParent();
							tabGroupActivityView.startChildActivity(
									"ActivityCategories", intent_view_monan);

						} else if (pos == 2) { // Upload item selected
							Intent intent_view_monan = new Intent(self,
									ActivityPayment.class);
							TabGroupActivity tabGroupActivityView = (TabGroupActivity) getParent();
							tabGroupActivityView.startChildActivity(
									"ActivityPayment", intent_view_monan);

						} else if (pos == 3) { // Upload item selected
							Intent intent_view_monan = new Intent(self,
									ActivityFaq.class);
							TabGroupActivity tabGroupActivityView = (TabGroupActivity) getParent();
							tabGroupActivityView.startChildActivity(
									"ActivityFaq", intent_view_monan);
						} else if (pos == 4) { // Upload item selected
							Intent intent_view_monan = new Intent(self,
									ActivityContactUs.class);
							TabGroupActivity tabGroupActivityView = (TabGroupActivity) getParent();
							tabGroupActivityView.startChildActivity(
									"ActivityContactUs", intent_view_monan);
						}
					}
				});

	}

	public OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_menu:
				mQuickAction.show(v);
				mQuickAction.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);
				break;
			case R.id.btn_privacy:
				Intent intent_privacy = new Intent(self,
						ActivityPolicy.class);
				TabGroupActivity tabPrivacy = (TabGroupActivity) getParent();
				tabPrivacy.startChildActivity(
						"ActivityPolicy", intent_privacy);
				break;
			case R.id.btn_wholesale:
				Intent intent_wholesale = new Intent(self,
						ActivityWholesale.class);
				
				TabGroupActivity tabsWholesale = (TabGroupActivity) getParent();
				tabsWholesale.startChildActivity(
						"ActivityWholesale", intent_wholesale);
				break;
			case R.id.btn_shipping:
				Intent intent_shipping = new Intent(self,
						ActivityShipping.class);
				TabGroupActivity tabShipping = (TabGroupActivity) getParent();
				tabShipping.startChildActivity(
						"ActivityShipping", intent_shipping);
				

				
				break;
			default:
				break;
			}

		}
	};

	/**
	 * Checks if is empty.
	 * 
	 * @param str
	 *            the str
	 * @return true, if is empty
	 */
	public boolean isEmpty(String str) {
		return TextUtils.isEmpty(str);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * Check network connection.
	 * 
	 * @return true, if is network available
	 */
	public boolean isNetworkAvailable() {
		ConnectivityManager conMgr = (ConnectivityManager) self
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo i = conMgr.getActiveNetworkInfo();
		if (i == null) {
			return false;
		}
		if (!i.isConnected()) {
			return false;
		}
		if (!i.isAvailable()) {
			return false;
		}
		return true;
	}

	/**
	 * Go to other activity.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 */
	protected void gotoActivity(Context context, Class<?> cla) {
		Intent intent = new Intent(context, cla);
		startActivity(intent);
	}

	/**
	 * Goto activity.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param flag
	 *            the flag
	 */
	protected void gotoActivity(Context context, Class<?> cla, int flag) {
		Intent intent = new Intent(context, cla);
		intent.setFlags(flag);
		startActivity(intent);
	}

	/**
	 * Goto activity with bundle.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param bundle
	 *            the bundle
	 */
	protected void gotoActivity(Context context, Class<?> cla, Bundle bundle) {
		Intent intent = new Intent(context, cla);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	/**
	 * Goto activity for result.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param reqCode
	 *            the req code
	 */
	protected void gotoActivityForResult(Context context, Class<?> cla,
			int reqCode) {
		Intent intent = new Intent(context, cla);
		startActivityForResult(intent, reqCode);

	}

	/**
	 * Goto activity.
	 * 
	 * @param context
	 *            the context
	 * @param cla
	 *            the cla
	 * @param flag
	 *            the flag
	 * @param bundle
	 *            the bundle
	 */
	protected void gotoActivity(Context context, Class<?> cla, int flag,
			Bundle bundle) {
		Intent intent = new Intent(context, cla);
		intent.setFlags(flag);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	// ======================= PROGRESS DIALOG ======================

	/**
	 * Open progress dialog.
	 * 
	 * @param text
	 *            the text
	 */
	protected void showProgressDialog(String text) {
		if (progressDialog == null) {
			progressDialog = ProgressDialog.show(this,
					getString(R.string.app_name), text, true);
			progressDialog.setCancelable(false);
		}
	}

	/**
	 * Close progress dialog.
	 */
	protected void closeProgressDialog() {
		if (progressDialog != null) {
			progressDialog.cancel();
			progressDialog = null;
		}
	}

	// ======================= KEYBOARD MANAGER =======================

	/**
	 * Show soft keyboard.
	 * 
	 * @param editText
	 *            the edit text
	 */
	protected void showKeyboard(EditText editText) {
		InputMethodManager inputMethodManager = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (inputMethodManager != null) {
			inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,
					0);
		}
	}

	/**
	 * Hide soft keyboard.
	 * 
	 * @param editText
	 *            the edit text
	 */
	protected void hideKeyboard(EditText editText) {
		((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
				.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	// =======================TOAST MANAGER===============================

	/**
	 * Show toast.
	 * 
	 * @param msg
	 *            the msg
	 * @param duration
	 *            the duration
	 */
	protected void showToast(String msg, int duration) {
		Toast.makeText(this, msg, duration).show();
	}

	/**
	 * Show toast.
	 * 
	 * @param resId
	 *            the res id
	 * @param time
	 *            the time
	 */
	protected void showToast(int resId, int time) {
		Toast.makeText(this, resId, time).show();
	}

	/**
	 * Show toast.
	 * 
	 * @param view
	 *            the view
	 * @param time
	 *            the time
	 */
	protected void showToast(View view, int time) {
		Toast toast = new Toast(this);
		toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
		toast.setView(view);
		toast.show();

	}

	// ======================= DIALOG MANAGER =======================

	/**
	 * Show confirm dialog.
	 * 
	 * @param messageId
	 *            the message id
	 * @param positiveLabelId
	 *            the positive label id
	 * @param negativeLabelId
	 *            the negative label id
	 * @param positiveOnClick
	 *            the positive on click
	 * @param negativeOnClick
	 *            the negative on click
	 */
	protected void showDialog(int messageId, int positiveLabelId,
			int negativeLabelId,
			DialogInterface.OnClickListener positiveOnClick,
			DialogInterface.OnClickListener negativeOnClick) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.app_name));
		builder.setMessage(getString(messageId));
		builder.setPositiveButton(getString(positiveLabelId), positiveOnClick);
		builder.setNegativeButton(getString(negativeLabelId), negativeOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show confirm dialog.
	 * 
	 * @param title
	 *            the title
	 * @param messageId
	 *            the message id
	 * @param positiveLabelId
	 *            the positive label id
	 * @param negativeLabelId
	 *            the negative label id
	 * @param positiveOnClick
	 *            the positive on click
	 * @param negativeOnClick
	 *            the negative on click
	 */
	protected void showDialog(String title, int messageId, int positiveLabelId,
			int negativeLabelId,
			DialogInterface.OnClickListener positiveOnClick,
			DialogInterface.OnClickListener negativeOnClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(getString(messageId));
		builder.setPositiveButton(getString(positiveLabelId), positiveOnClick);
		builder.setNegativeButton(getString(negativeLabelId), negativeOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show information dialog.
	 * 
	 * @param messageId
	 *            the message id
	 * @param positiveLabelId
	 *            the positive label id
	 * @param positiveOnClick
	 *            the positive on click
	 */
	protected void showDialog(int messageId, int positiveLabelId,
			DialogInterface.OnClickListener positiveOnClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.app_name));
		builder.setMessage(getString(messageId));
		builder.setPositiveButton(getString(positiveLabelId), positiveOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show delete message dialog.
	 * 
	 * @param positiveOnClick
	 *            the positive on click
	 * @param negativeOnClick
	 *            the negative on click
	 * @param idMessage
	 *            the id message
	 */
	protected void showDeleteDialog(
			DialogInterface.OnClickListener positiveOnClick,
			DialogInterface.OnClickListener negativeOnClick, int idMessage) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(idMessage));
		builder.setPositiveButton(getString(android.R.string.yes),
				positiveOnClick);
		builder.setNegativeButton(getString(android.R.string.no),
				negativeOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Show single Dialog.
	 * 
	 * @param resId
	 *            resId
	 */
	protected void showSingleDialog(int resId) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(resId));
		builder.setPositiveButton(android.R.string.ok, null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}
}
